package com.sky;

import org.apache.commons.lang.text.StrBuilder;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * CodeGeneration
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/7/27 20:39
 */
public class CodeGeneration {
    private final static String SQL = "type, name, sort, status, create_time, update_time, create_user, update_user";

    public static void main(String[] args) {
        // 新增SQL生成
        System.out.println("-----------------新增SQL-----------------");
        generateInsert(SQL);
        System.out.println("-----------------新增SQL-----------------");

        // 查询SQL生成
        System.out.println("------------------查询SQL----------------");
        generateSelect(SQL);
        System.out.println("------------------查询SQL----------------");

        // 更新SQL生成
        System.out.println("------------------更新SQL----------------");
        generateUpdate(SQL);
        System.out.println("------------------更新SQL----------------");
    }

    private static void generateUpdate(String sql) {
        String[] fields = sql.replaceAll(" ", "").split(",");
        StrBuilder builder = new StrBuilder();
        builder.append("<if test=\"%s!= null\">");
        builder.append("%s = #{%s},");
        builder.append("</if>");
        String collect = Arrays.stream(fields).map(field -> {
            if (field.contains("_")) {
                String[] split = field.split("_");
                String attributeLower = Arrays.stream(split)
                        .map(item -> item.substring(0, 1).toUpperCase() + item.substring(1))
                        .collect(Collectors.joining());
                String attribute = attributeLower.substring(0, 1).toLowerCase() + attributeLower.substring(1);
                return String.format(builder.toString(), attribute, field, attribute);
            }
            return String.format(builder.toString(), field, field, field);
        }).collect(Collectors.joining("\n"));
        System.out.println(collect);
    }

    private static void generateSelect(String sql) {
        String[] fields = sql.replaceAll(" ", "").split(",");
        StrBuilder builder = new StrBuilder();
        builder.append("<if test=\"%s!= null\">");
        builder.append("and %s = #{%s}");
        builder.append("</if>");
        String collect = Arrays.stream(fields).map(field -> {
            if (field.contains("_")) {
                String[] split = field.split("_");
                String attributeLower = Arrays.stream(split)
                        .map(item -> item.substring(0, 1).toUpperCase() + item.substring(1))
                        .collect(Collectors.joining());
                String attribute = attributeLower.substring(0, 1).toLowerCase() + attributeLower.substring(1);
                return String.format(builder.toString(), attribute, field, attribute);
            }
            return String.format(builder.toString(), field, field, field);
        }).collect(Collectors.joining("\n"));
        System.out.println(collect);

    }

    /**
     * 生成插入
     *
     * @param sql sql
     */
    private static void generateInsert(String sql) {
        String[] fields = sql.replaceAll(" ", "").split(",");
        String collect = Arrays.stream(fields).map(field -> {
            if (field.contains("_")) {
                String[] split = field.split("_");
                field = Arrays.stream(split)
                        .map(item -> item.substring(0, 1).toUpperCase() + item.substring(1))
                        .collect(Collectors.joining());
                field = field.substring(0, 1).toLowerCase() + field.substring(1);
            }
            return String.format("#{%s}", field);
        }).collect(Collectors.joining(","));
        System.out.println(collect);
    }
}
